package cn.iocoder.yudao.module.erp.controller.admin.suppliersendorderitem;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.suppliersendorderitem.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.suppliersendorderitem.SupplierSendOrderItemDO;
import cn.iocoder.yudao.module.erp.service.suppliersendorderitem.SupplierSendOrderItemService;

@Tag(name = "管理后台 - ERP 随货同行单项")
@RestController
@RequestMapping("/erp/supplier-send-order-item")
@Validated
public class SupplierSendOrderItemController {

    @Resource
    private SupplierSendOrderItemService supplierSendOrderItemService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 随货同行单项")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order-item:create')")
    public CommonResult<Long> createSupplierSendOrderItem(@Valid @RequestBody SupplierSendOrderItemSaveReqVO createReqVO) {
        return success(supplierSendOrderItemService.createSupplierSendOrderItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 随货同行单项")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order-item:update')")
    public CommonResult<Boolean> updateSupplierSendOrderItem(@Valid @RequestBody SupplierSendOrderItemSaveReqVO updateReqVO) {
        supplierSendOrderItemService.updateSupplierSendOrderItem(updateReqVO);
        return success(true);
    }

    @PutMapping("/updates")
    @Operation(summary = "更新ERP 随货同行单项")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order-item:update')")
    public CommonResult<Boolean> updateSupplierSendOrderItems(@Valid @RequestBody List<SupplierSendOrderItemSaveReqVO> updateReqVOList) {
        supplierSendOrderItemService.updateSupplierSendOrderItems(updateReqVOList);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 随货同行单项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order-item:delete')")
    public CommonResult<Boolean> deleteSupplierSendOrderItem(@RequestParam("id") Long id) {
        supplierSendOrderItemService.deleteSupplierSendOrderItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 随货同行单项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order-item:query')")
    public CommonResult<SupplierSendOrderItemRespVO> getSupplierSendOrderItem(@RequestParam("id") Long id) {
        SupplierSendOrderItemDO supplierSendOrderItem = supplierSendOrderItemService.getSupplierSendOrderItem(id);
        return success(BeanUtils.toBean(supplierSendOrderItem, SupplierSendOrderItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 随货同行单项分页")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order-item:query')")
    public CommonResult<PageResult<SupplierSendOrderItemRespVO>> getSupplierSendOrderItemPage(@Valid SupplierSendOrderItemPageReqVO pageReqVO) {
        PageResult<SupplierSendOrderItemDO> pageResult = supplierSendOrderItemService.getSupplierSendOrderItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplierSendOrderItemRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 随货同行单项 Excel")
    @PreAuthorize("@ss.hasPermission('erp:supplier-send-order-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSupplierSendOrderItemExcel(@Valid SupplierSendOrderItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplierSendOrderItemDO> list = supplierSendOrderItemService.getSupplierSendOrderItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 随货同行单项.xls", "数据", SupplierSendOrderItemRespVO.class,
                        BeanUtils.toBean(list, SupplierSendOrderItemRespVO.class));
    }



}